package app

import (
	"gin-wheel/pkg/errcode"
	"net/http"

	"github.com/gin-gonic/gin"
)

type response struct {
	Ctx *gin.Context
}

func NewResponse(ctx *gin.Context) *response {
	return &response{
		Ctx: ctx,
	}
}

func (r *response) ToResponse(err *errcode.Error, data interface{}) {
	response := gin.H{"code": err.Code, "msg": err.Msg}
	if data != nil {
		response = gin.H{"code": err.Code, "msg": err.Msg, "data": data}
	}
	r.Ctx.JSON(http.StatusOK, response)
}
